package com.yash.rms.domain;

import com.yash.rms.util.Constants;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "timehrs")
@RooDbManaged(automaticallyDelete = true)
@RooJson
public class Timehrs implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "resource_id", referencedColumnName = "employee_id", nullable = false)
    private Resource resourceId;

    public Resource getResourceId() {
        return resourceId;
    }

    public void setResourceId(Resource resourceId) {
        this.resourceId = resourceId;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").transform(new DateTransformer(Constants.DATE_PATTERN), Date.class).serialize(this);
    }

    public static com.yash.rms.domain.Timehrs fromJsonToTimehrs(String json) {
        return new JSONDeserializer<Timehrs>().use(null, Resource.class).use(Date.class, new DateTransformer(Constants.DATE_PATTERN)).deserialize(json);
    }

    public static String toJsonArray(Collection<com.yash.rms.domain.Timehrs> collection) {
        return new JSONSerializer().exclude("*.class").transform(new DateTransformer(Constants.DATE_PATTERN), Date.class).serialize(collection);
    }

    public static Collection<com.yash.rms.domain.Timehrs> fromJsonArrayToTimehrses(String json) {
        return new JSONDeserializer<List<Timehrs>>().use(null, ArrayList.class).use(Date.class, new DateTransformer(Constants.DATE_PATTERN)).use("values", Resource.class).deserialize(json);
    }
}
